package com.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.helper.DBHelper;
import com.model.QuestionChoice;

public class QuestionChoiceDao {
	
	private QuestionChoice toQuestionChoice(Map<String, Object> data) 
	{
		QuestionChoice q = new QuestionChoice();
		q.setId((Integer)data.get("id"));
		q.setContent((String)data.get("content"));
		q.setAnswer((String)data.get("answer"));
		if(data.get("A") != null)
			q.setAnsA((String)data.get("A"));
		if(data.get("B") != null)
			q.setAnsB((String)data.get("B"));
		if(data.get("C") != null)
			q.setAnsC((String)data.get("C"));
		if(data.get("D") != null)
			q.setAnsD((String)data.get("D"));
		if(data.get("E") != null)
			q.setAnsE((String)data.get("E"));
		if(data.get("F") != null)
			q.setAnsF((String)data.get("F"));
		return q;
	}
	
	public List<QuestionChoice> getAll() 
	{
		return getAll(0); 
	}
	
	//type=0 return All  
	//type=1 return single
	//type=2 return multi
	public List<QuestionChoice> getAll(int type) 
	{
		List<QuestionChoice> result = new ArrayList<QuestionChoice>();
		String sql = "select * from que_choice";
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			if(type == 0) {
				result.add(toQuestionChoice(ele));
			} else {
				String ans = (String)ele.get("answer");
				ans.replace(",", "");	
				boolean isSingle = ans.length() == 1;
				if(type == 1 && isSingle)
					result.add(toQuestionChoice(ele));
				else if(type == 2 && !isSingle)
					result.add(toQuestionChoice(ele));
			}	
		}
		return result;
	}

	public List<QuestionChoice> getAllSingle()
	{
		return getAll(1); 
	}
	
	public List<QuestionChoice> getAllMulti()
	{
		return getAll(2); 
	}

	public Boolean edit(int qid, String cont, List<String> choice, String ans) {
		String sql = "UPDATE `que_choice` SET"
				+ "`content` = ?, `A` = ?, `B` = ?, `C` = ?, `D` = ?, `E` = ?, `F` = ?, `answer` = ? WHERE `id` = ?;";
		List<Object> param = new ArrayList<>();
		param.add(cont);
		param.add(choice.get(0));
		param.add(choice.get(1));
		param.add(choice.get(2));
		param.add(choice.get(3));
		param.add(choice.get(4));
		param.add(choice.get(5));
		param.add(ans);
		param.add(qid);
		DBHelper db = new DBHelper();
		return db.update(sql, param);
	}

	public Boolean updateAnswer(int qid, String ans) {
		String sql = "UPDATE `que_choice` SET `answer` = ? WHERE `id` = ?;";
		List<Object> param = new ArrayList<>();
		param.add(ans);
		param.add(qid);
		DBHelper db = new DBHelper();
		return db.update(sql, param);
	}

	public Boolean add(String cont, List<String> choice, String ans) {
		String choiceStr = "";
		for(int i=0; i<choice.size(); ++i) {
			choiceStr += "'" + choice.get(i) + "',";
		}
		for(int i=choice.size(); i<6; ++i) {
			choiceStr += "'',";
		}
		String sql = "INSERT INTO `que_choice` "
				+ "(`content`, `A`, `B`, `C`, `D`, `E`, `F`, `answer`) "
				+ "VALUES ('"  + cont +  "'," + choiceStr   + "'" + ans.toUpperCase() +    "')";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public QuestionChoice getById(Integer queId) {
		QuestionChoice result = new QuestionChoice();
		String sql = "select * from que_choice where id=" + queId;
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result = toQuestionChoice(ele);
		}
		return result;
	}
	
}
